package tid.proyect.models;

import java.io.InputStream;

import tid.proyect.controllers.EditImage;
import tid.proyect.md.R;
import tid.proyect.md.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

public class OpenImage extends Activity {
	Context c;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		c = this;
		setContentView(R.layout.image);
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		photoPickerIntent.setType("image/*");
		startActivityForResult(photoPickerIntent, 1);
	}
	
	// We go to the gallery to obtained the image
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == RESULT_OK) {
			Uri chosenImageUri = data.getData();
			if (chosenImageUri != null) {
				try {
					InputStream photoStream = getContentResolver().openInputStream(chosenImageUri);
					BitmapFactory.Options opts = new BitmapFactory.Options();
					opts.inSampleSize = 4;
					Bitmap mBitmap = BitmapFactory.decodeStream(photoStream, null, opts);
					if (mBitmap != null) {
						EditImage.setJpegData(mBitmap);
					}
					photoStream.close();
					//When we choose a image, we can call EditImage for compare
					startActivity(new Intent(c, EditImage.class));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}